package com.eascs.archive.controller.v0;


import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONObject;
import com.eascs.admin.common.vo.LoanApplyEntity;
import com.eascs.admin.common.vo.ModelResult;
import com.eascs.admin.o2o.internal.entity.PubDditem;
import com.eascs.admin.o2o.internal.service.PubDditemService;
import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.admin.o2o.loan.service.O2oLoanapplyService;
import com.eascs.archive.dubbo.ArchiveTask;
import com.eascs.archive.entity.ArchiveRecordEntity;
import com.eascs.archive.entity.FieldEntity;
import com.eascs.archive.service.ArchiveRecordService;
import com.eascs.archive.service.ArchiveService;
import com.eascs.archive.untils.ArchiveConstants;
import com.eascs.datalist.entity.O2oDataListEntity;
import com.eascs.datalist.service.O2oDataListService;
import com.eascs.dubbo.loan.service.LoanInfoServiceImpl;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;

@RestController(value="archiveRestController")
@RequestMapping("/archive/rest")
public class ArchiveRestController extends BaseRestController {
	
	@Autowired
	private ArchiveRecordService archiveRecordService;
	@Autowired
	private ArchiveService archiveService;
	@Autowired
	private LoanInfoServiceImpl loanInfoService;
	@Autowired
	private O2oDataListService o2oDataListService;
	@Autowired
	private ArchiveTask archiveTask;
	@Autowired
	private O2oLoanapplyService o2oLoanapplyService;
	@Autowired
	private PubDditemService pubDditemService;
	
	
	

	
	@RequestMapping(value = "/platformManager", method = RequestMethod.GET)
	public HtmlRestResult platformManager(String referId) {
		String loanapplyNo=referId;
		ModelResult<LoanApplyEntity> modelResult = loanInfoService.getLoanapplyInfo(loanapplyNo);
		LoanApplyEntity loanInfo = modelResult.getModel();
		String loanId=loanInfo.getLoanId();
		String bankName = loanInfo.getBankLoanName();
		String bankTypeId = ArchiveConstants.getBankTypeId(bankName);
		if (StringUtils.isNotBlank(bankTypeId)) {
			List<FieldEntity> archiveFields = archiveService.getFieldListByType(bankTypeId, ArchiveConstants.FORMTYPE_ARCHIVE);
			ArchiveRecordEntity archiveRecord=getFieldValue(archiveFields, loanapplyNo, bankTypeId, ArchiveConstants.FORMTYPE_ARCHIVE);
			//判断是否保存
			if(archiveFields!=null&&archiveRecord!=null&&archiveFields.size()>0){
				ViewContext.put("isSave", "show");
			}else{
				ViewContext.put("isSave", "hidden");
			}
			ViewContext.put("archiveFields", archiveFields);
			ViewContext.put("archiveRecord", archiveRecord);
			ViewContext.put("bankTypeId", bankTypeId);
			ViewContext.put("loanapplyNo", loanapplyNo);
			ViewContext.put("loanId", loanId);
			ViewContext.put("formTypeId", ArchiveConstants.FORMTYPE_ARCHIVE);
		}
		return HtmlRestResult.html("archive/v0/platformManager");
	}
	
	@RequestMapping(value = "/reviewHQ", method = RequestMethod.GET)
	public HtmlRestResult reviewHQ(String referId) {
		String loanapplyNo=referId;
		ModelResult<LoanApplyEntity> modelResult = loanInfoService.getLoanapplyInfo(loanapplyNo);
		LoanApplyEntity loanInfo = modelResult.getModel();
		String bankName = loanInfo.getBankLoanName();
		String bankTypeId = ArchiveConstants.getBankTypeId(bankName);
		if (StringUtils.isNotBlank(bankTypeId)) {
			List<FieldEntity> archiveFields = archiveService.getFieldListByType(bankTypeId, ArchiveConstants.FORMTYPE_ARCHIVE);
			ArchiveRecordEntity archiveRecord=getFieldValue(archiveFields, loanapplyNo, bankTypeId, ArchiveConstants.FORMTYPE_ARCHIVE_HQ_CHECK);
			if (archiveRecord == null ) {
				getFieldValue(archiveFields, loanapplyNo, bankTypeId, ArchiveConstants.FORMTYPE_ARCHIVE);
			}
			ViewContext.put("archiveFields", archiveFields);
			ViewContext.put("bankTypeId", bankTypeId);
			ViewContext.put("loanapplyNo", loanapplyNo);
			ViewContext.put("formTypeId", ArchiveConstants.FORMTYPE_ARCHIVE_HQ_CHECK);
		}
		return HtmlRestResult.html("archive/v0/reviewHQ");
	}
	
	
	@RequestMapping(value = "/archiveHQ", method = RequestMethod.GET)
	public HtmlRestResult archiveHQ(String referId) {
		_archiveHQ(referId);
		ViewContext.put("showSaveBtn", true);
		return HtmlRestResult.html("archive/v0/archiveHQ");
	}
	
	@RequestMapping(value = "/archiveHQPopup", method = RequestMethod.GET)
	public HtmlRestResult archiveHQPopup(String referId) {
		_archiveHQ(referId);
		return HtmlRestResult.popHtmlFragment("archive/v0/archiveHQ");
	}
	
	
	private void _archiveHQ(String referId){
		String loanapplyNo=referId;
		ModelResult<LoanApplyEntity> modelResult = loanInfoService.getLoanapplyInfo(loanapplyNo);
		LoanApplyEntity loanInfo = modelResult.getModel();
		String bankName = loanInfo.getBankLoanName();
		String bankTypeId = ArchiveConstants.getBankTypeId(bankName);
		if (StringUtils.isNotBlank(bankTypeId)) {
			List<FieldEntity> bankFields = archiveService.getFieldListByType(bankTypeId, ArchiveConstants.FORMTYPE_BANK);
			List<FieldEntity> financeFields = archiveService.getFieldListByType(bankTypeId, ArchiveConstants.FORMTYPE_FINANCE);
			List<FieldEntity> archiveFields = archiveService.getFieldListByType(bankTypeId, ArchiveConstants.FORMTYPE_ARCHIVE_INFO);
			getFieldValue(bankFields, loanapplyNo, bankTypeId, ArchiveConstants.FORMTYPE_BANK);
			getFieldValue(financeFields, loanapplyNo, bankTypeId, ArchiveConstants.FORMTYPE_FINANCE);
			ArchiveRecordEntity archiveRecord=getFieldValue(archiveFields, loanapplyNo, bankTypeId, ArchiveConstants.FORMTYPE_ARCHIVE_INFO);
			ViewContext.put("bankFields", bankFields);
			ViewContext.put("financeFields", financeFields);
			ViewContext.put("archiveFields", archiveFields);
			ViewContext.put("archiveRecord", archiveRecord);
			ViewContext.put("bankTypeId", bankTypeId);
			ViewContext.put("loanapplyNo", loanapplyNo);
			ViewContext.put("FORMTYPE_BANK", ArchiveConstants.FORMTYPE_BANK);
			ViewContext.put("FORMTYPE_FINANCE", ArchiveConstants.FORMTYPE_FINANCE);
			ViewContext.put("FORMTYPE_ARCHIVE_INFO", ArchiveConstants.FORMTYPE_ARCHIVE_INFO);
		}
	}
	
	@RequestMapping("/fileDiv")
	public HtmlRestResult getFileDiv(String loanapplyNo,String loanId){
		ViewContext.put("loanapplyNo", loanapplyNo);
		ViewContext.put("loanId", loanId);
		/**** 查询附件 ****/
		List<O2oDataListEntity> o2oDataList = o2oDataListService.findByEntityList(loanapplyNo);
		ViewContext.put("o2oDataList", o2oDataList);
		return HtmlRestResult.html("archive/v0/uploadDiv");
	}

	public ArchiveRecordEntity getFieldValue(List<FieldEntity> fields, String loanapplyNo, String bankTypeId, String fromTypeId) {
		ModelResult<ArchiveRecordEntity> modelResult = archiveRecordService.queryArchiveRecord(loanapplyNo, bankTypeId,fromTypeId);
		ArchiveRecordEntity model = modelResult.getModel();
		if (model != null) {
			JSONObject obj = JSONObject.parseObject(model.getContentJson());
			for (FieldEntity f : fields) {
				String fieldValue = obj.getString(f.getFieldName());
				f.setFieldValue(fieldValue);
			}
		}
		return model;
	}
	

	@RequestMapping(value = "/saveArchiveRecord", method = RequestMethod.POST)
	public BaseRestResult<String> saveArchiveRecord(ArchiveRecordEntity entity){
		return save(entity);
	}
	
	@RequestMapping(value = "/saveArchiveRecords", method = RequestMethod.POST)
	@Transactional(transactionManager = "o2oTransactionManager")
	public BaseRestResult<String> saveArchiveRecords(String archiveRecordJSON){
		try {
			if (StringUtils.isBlank(archiveRecordJSON)) {
				return BaseRestResult.warn("参数不能为空");
			}
			JSONObject obj=JSONObject.parseObject(archiveRecordJSON);
			ArchiveRecordEntity bank=obj.getObject(ArchiveConstants.FORMTYPE_BANK, ArchiveRecordEntity.class);
			ArchiveRecordEntity finance=obj.getObject(ArchiveConstants.FORMTYPE_FINANCE, ArchiveRecordEntity.class);
			ArchiveRecordEntity archiveInfo=obj.getObject(ArchiveConstants.FORMTYPE_ARCHIVE_INFO, ArchiveRecordEntity.class);
			save(bank);
			save(finance);
			save(archiveInfo);
			return BaseRestResult.info("保存成功");
		} catch (Exception e) {}
		return BaseRestResult.warn("保存失败，请联系管理员");
	}
	
	private BaseRestResult<String> save(ArchiveRecordEntity entity){
		User user = UserHolder.getUser();
		ModelResult<ArchiveRecordEntity> modelResult =archiveRecordService.queryArchiveRecord(entity.getLoanapplyNo(), entity.getBankTypeId(), entity.getFormTypeId());
		ArchiveRecordEntity model=modelResult.getModel();
		entity.setUpdator(user.getAccount());
		if (model == null) {
			entity.setCreator(user.getAccount());
			ModelResult<Boolean> saveResult =archiveRecordService.insertArchiveRecord(entity);
			Boolean result=saveResult.getModel();
			if(result){
				return BaseRestResult.info("保存成功");
			}
		}else{
			ModelResult<Boolean> updateResult =archiveRecordService.updateArchiveRecord(entity);
			Boolean result=updateResult.getModel();
			if(result){
				return BaseRestResult.info("保存成功");
			}
		}
		return BaseRestResult.warn("保存失败，请联系管理员");
	}
	
	@RequestMapping(value = "/startArchive", method = RequestMethod.GET)
	public BaseRestResult<String> startArchive(String loanapplyNo){
		try {
			if (StringUtils.isBlank(loanapplyNo)) {
				return BaseRestResult.info("贷款no不能为空!");
			}
			O2oLoanapplyEntity loan = o2oLoanapplyService.findById(loanapplyNo);
			String isConfig = ArchiveConstants.getBankTypeId(loan.getBankLoanName());
			if (StringUtils.isBlank(isConfig)) {
				PubDditem pubDditem = pubDditemService.queryPubDditem("10340",loan.getBankLoanName());
				return BaseRestResult.info("目前暂不支持"+pubDditem.getItemName()+"银行贷后归档，开始归档时间请留意总部通知");
			}
			com.eascs.workflow.common.vo.ModelResult<Boolean> modelResult= archiveTask.startUp(loanapplyNo);
			if (modelResult.isSuccess()) {
				return BaseRestResult.info("档案归档发起成功").andRefresh();
			}
			return BaseRestResult.warn("档案归档发起失败,"+modelResult.getErrMsg());
		} catch (Exception e) {
			return BaseRestResult.warn("档案归档发起失败，请联系管理员");
		}
		
	}
}
